{% extends "base.html" %}
{% block header-content %}
<h3>Session</h3>
<p>Session is a class to manage persistent data across multiple page views. It includes user IP and User Agent validation, as well as session token regeneration to manage session fixation vulnerabilities. Only the session id is stored via cookie, all other data is stored on server side. Session data can be set using standard Python container access methods (Session()["key"] = "value").
</p>
{% endblock %}
{% block content %}
    {% if sess.flash %}
    <p>{{ sess.flash }}</p>
    {% endif %}
    <p> Welcome to the sessions demo. You've viewed this page <strong>{{ sess.viewCount }}</strong> times during this session. You session id is <strong>{{ sess.sid }}</strong> There are {{ session_length }} objects in your session data. The keys are: <ul>
{% for k in sess %}
<li>{{ k }}</li>
{% endfor %}
When the Django template library is upgraded this demo can be modified to show the values as well. For now this is enough to demonstrate iteration support.
</ul><br />
                You can <a href="/session">View this page again</a>, you can <a href="/session?setflash=true">Set flash data</a>, pr you can <a href="/session?deleteSession=true">Delete your session</a> and start over.</p>
<p>Memcache stats: {{ memcacheStats }}
<div class="code">
<h2>webapp class</h2>
<pre>{% filter escape %}class SessionPage(webapp.RequestHandler):
class SessionPage(webapp.RequestHandler):
  def get(self):
    self.sess = sessions.Session()
    if self.request.get('deleteSession') == "true":
        self.sess.delete()
        print "Location: /session\n\n"
    elif self.request.get('setflash') == "true":
        self.sess['flash'] = 'You set a flash message! <a href="/session">Refresh this page</a> and this message is gone!'
        print "Location: /session\n\n"
    else:
        self.sess['testKey'] = "test"
        self.sess['testKey2'] = "test2"
        if not 'viewCount' in self.sess:
            self.sess['viewCount'] = 1
        else:
            self.sess['viewCount'] = int(self.sess['viewCount']) + 1
        self.memcacheStats = memcache.get_stats()
        template_values = {
            'sess': self.sess,
            'memcacheStats': self.memcacheStats
        }
        path = os.path.join(os.path.dirname(__file__), 'templates/session.html')
        self.response.out.write(template.render(path, template_values))
{% endfilter %}</pre>
</div>
<div class="code">
<h2>template</h2>
<pre>
{% filter escape %}{% templatetag openblock %} if sess.flash {% templatetag closeblock %}
    <p>{% templatetag openvariable %} sess.flash {% templatetag closevariable %}</p>
    {% templatetag openblock %} endif {% templatetag closeblock %}
<p> Welcome to the sessions demo. You've viewed this page <strong>{% templatetag openvariable %} sess.viewCount {% templatetag closevariable %}</strong> times during this session. You session id is <strong>{% templatetag openvariable %} sess.sid }}</strong><br />
You can <a href="/session">View this page again</a>, you can <a href="/session?setflash=true">Set flash data</a>, pr you can <a href="/session?deleteSession=true">Delete your session</a> and start over.</p>
{% endfilter %}</pre>
</div>
{% endblock %}
